<?php
namespace app\index\model;
use think\Model;
use think\Session;
class Orders extends Model
{
    protected $autoWriteTimestamp = 'timestamp';
    protected $createTime = 'created_at';
    protected $updateTime = 'updated_at';
    protected $table = 'orders';
    protected $type = [
        'rent_contact_id'=>'array'
    ];
    const OrderCancelled = 103;//订单已取消
    use \app\common\traits\model\TraitModel;


    protected function notifys($msg, $order_info)
    {
        $user = self::getLoginUserInfo();
        if ($user->open_id && $user->oauth_type == 'wx') {
            $notice = new \app\common\Notice();
            $data = array(
                     "first"  => '您的车辆已经预定成功',
                     "OrderID"   => $order_info->number,
                     "PersonName" => '人数暂定',
                     "PickupDate" => $order_info->take_car_date,
                     "DropoffDate" => $order_info->leave_car_date,
                     "CarType" => '房车',
                     "PaymentType" => '暂定',
                     "remark" => "订单将为您保留1小时，请尽快完成支付",
                    );
            $notice->setTemplate('ordercreate')->send($user->open_id, $data, 'http://wx.shsytour.cn/order/show?order_id=' . $order_info->number);
        }
        model('user/Notifys')->createNotifys(['users_id' => $user->id, 'body' => $msg]);
    }
    /**
     * 获取订单
     * @param array $query 查询条件
     * @return [type] [description]
     */
    public function getOrders($query = array(), $order = 'updated_at desc')
    {
        $query = array_merge($query, ['rent_user_id' => $this->getLoginUserInfo('id')]);
        return $this->setOrder($order)->getInfoList($query);
    }
    /**
     * 获取订单信息
     * @param  string $sn      订单号
     * @param  uuid $user_id 用户id
     * @return Model          订单模型
     */
    public function getOrderSn($sn, $user_id)
    {
        return $this->failException(true)->where(['number' => $sn ,'rent_user_id' =>$user_id])->find();
    }
    /**
     * 生成订单SN
     * @return [type] [description]
     */
    public function createOrderSn()
    {
        return $this->getBusiness() . substr(time(), 0, 8) . substr(str_shuffle(str_repeat('0123456789', 6)), 0, 6);
    }
    private function getBusiness()
    {
        return 1;
    }

    /**
     * [getIntergral 获取总旅行币]交换获取总旅行币
     * @param  [type] $days [description]
     * @return [type]       [description]
     */
    public function getIntergral($sdate,$edate,$travel_money_base)
    {
        $stime=strtotime($sdate);//开始时间
        $etime=strtotime($edate);//结束时间
        while($stime <= $etime){
        $datetime[] = date('Y-m-d',$etime);//得到datetime的日期数组。
        $etime=$etime - 3600*24;
        }  
        $total = $this->getCoefficient($datetime);
        $intergral = $travel_money_base*array_sum($total);
        return $intergral;
    }
    /**
     * [FunctionName 获取出租总钱数]
     * @param string $value [description]
     */
    public function getMoney($caravan_id,$sdate,$edate)
    {
        $mon = model('Calendar')->where('caravan_id', $caravan_id)->where('datetimes', 'between', [$sdate, $edate])->column('single_day_price');
        $money = array_sum($mon);
        return $money;
    }
    /**
     * 生成订单
     * @param  [type] $order_info_array [description]
     * @return [type]                   [description]
     */
    public function createOrder($order_info_array)
    {
        //halt($order_info_array);
        $caravan_id = $order_info_array['caravan_id'];
        $login_user_id = 2;
        $take_car_date = $order_info_array['take_car_date'];
        $leave_car_date = $order_info_array['leave_car_date'];
        $source = $order_info_array['source'];
        try {
                //创建订单
                $order = $this->create($order_info_array);
                //同步日历
                model('Calendar')->ordered($caravan_id, $take_car_date, $leave_car_date,$source);
                $this->commit();     
            } catch (Exception $e) {
                $this->rollback();
                return false;
            }
            $this->notifys('订单将为您保留1小时，请尽快完成支付', $order);
            return true; 
    }
    /**
     * [cancelOrder 取消订单]未付款
     * @return [type] [description]
     */
    public function cancelOrder($number)
    {
        $order = $this->where('number',$number)->find();
        try {
                //修改订单状态
                $this->where('number',$number)->setField('status',self::OrderCancelled);
                //同步日历
                model('Calendar')->delordered($order->caravan_id, $order->take_car_date, $order->leave_car_date,$order->source);
                $this->commit();
            } catch (Exception $e) {
                $this->rollback();
                return false;
            }
            return true;
    }

}
